<?php

/**
 * @author Aldis Rameika
 */
class ForeseeDesign {

    public static function show_input($subdivisions, $dates, $pre_entered_values) {
        ?>
        <form id="new_measure" class="new_measure" action="?foresee" method="post">
            <div class="tabber" id="foresee_tabber">
                <?
                $tabs_count = null;
                if ($pre_entered_values) {
                    $tabs_count = count($pre_entered_values["new_measure"]);

                    $measures = array();
                    foreach ($pre_entered_values["new_measure"] as $measure => $data) {
                        $measures[count($measures)] = $measure;
                    }
                    for ($tab_number = 0; $tab_number < $tabs_count; $tab_number++) {
                        if ($tab_number == 0) {
                            echo '<div class="tabbertab">';
                            echo '<h2>Nauja1</h2>';
                        } else {
                            echo '<div class="tabbertab tabbertabhide">';
                            echo '<h2>Nauja' . ($tab_number + 1) . '</h2>';
                        }
                        ?>
                        <div id="new_app_table_content">
                            <div id="new_app_table">
                                <p>Paraiškos administravimo laikas</p>
                                <table class="new_application_table">
                                    <?
                                    self::createAppInput($subdivisions, $pre_entered_values, $measures, $tab_number);
                                    ?>
                                </table>
                            </div>
                            <div id="new_app_quant_table">
                                <p>Planuojami paraiškos kiekiai</p>
                                <table class="new_app_quantities_table">
                                    <?
                                    self::createQuantitiesInput($dates, $pre_entered_values, $measures, $tab_number);
                                    ?>
                                </table>
                            </div>
                        </div>
                    </div>
                    <?
                }
            } else {
                ?>
                <div class="tabbertab">
                    <h2>Nauja1</h2>
                    <div id="new_app_table_content">
                        <div id="new_app_table">
                            <p>Paraiškos administravimo laikas</p>
                            <table class="new_application_table">
                                <?
                                self::createAppInput($subdivisions, null, null, 0);
                                ?>
                            </table>
                        </div>
                        <div id="new_app_quant_table">
                            <p>Planuojami paraiškos kiekiai</p>
                            <table class="new_app_quantities_table">
                                <?
                                self::createQuantitiesInput($dates, null, null, 0);
                                ?>
                            </table>
                        </div>
                    </div>
                </div>
                <?
            }
            ?>

            <div class="tabbertab">
                <h2>+</h2>
            </div>

        </div>
        <script>
        <?
        echo '$(document).ready(function() {
';
        if ($tabs_count) {
            echo 'setNewTabIndex(' . $tabs_count . ');';
            echo 'setTabIndex(' . ($tabs_count + 1) . ');';
        } else {
            echo 'setNewTabIndex(1);';
            echo 'setTabIndex(2);';
        }
        echo '});';
        ?>
                                                            
        </script>
        <div id="foresee_actions">
            <input type="reset" value="Atšaukti"/>
            <input type="submit" value="Parodyti prognozę"/>
        </div>
        </form>
        <?
    }

    public static function get_array_value($array, $key, $key2 = null, $key3 = null) {
        if ($key2 === null && array_key_exists($key, $array)) {
            return $array[$key];
        } else if ($key3 === null && array_key_exists($key, $array) && array_key_exists($key2, $array[$key])) {
            return $array[$key][$key2];
        } else if (array_key_exists($key, $array) && array_key_exists($key2, $array[$key]) && array_key_exists($key3, $array[$key][$key2])) {
            return $array[$key][$key2][$key3];
        } else {
            return '';
        }
    }

    private static function createAppInput($subdivisions, $pre_entered_values, $measures, $tab_number) {
        $sub_index = 0;
        foreach ($subdivisions as $code => $name) {
            $code = htmlspecialchars($code);
            $name = htmlspecialchars($name);
            echo "<tr>";
            if ($sub_index == 0) {
                echo "<td name='application_title'>Pavadinimas</td>";
                if ($pre_entered_values != null) {
                    echo "<td class='app_input'><input name='new_measure[n$tab_number]' title='Naujos paraiškos pavadinimas' value='" . $pre_entered_values["new_measure"][$measures[$tab_number]] . "'/ onkeyup='tab_name_changed(" . $tab_number . ")'></td>";
                } else {
                    echo "<td class='app_input'><input name='new_measure[n$tab_number]' title='Naujos paraiškos pavadinimas' value='Nauja1'/ onkeyup='tab_name_changed(0)'></td>";
                }
                echo "</tr>";
                echo "<tr>";
            }
            echo "<td sub='" . $sub_index . "'>" . $name . "</td>";
            if ($pre_entered_values != null) {
                echo "<td id='sub" . $sub_index . "'_input><input name='subdivision[n$tab_number][$code]' title='Paraiškos apdorojimo laikas padalnyje valandomis pvz: 0.5' value='" . $pre_entered_values["subdivision"][$measures[$tab_number]][$code] . "' onkeyup='nmFloat(this)'/></td>";
            } else {
                echo "<td id='sub" . $sub_index . "'_input><input name='subdivision[n$tab_number][$code]' title='Paraiškos apdorojimo laikas padalnyje valandomis pvz: 0.5' onkeyup='nmFloat(this)'/></td>";
            }

            echo "</tr>";
            $sub_index++;
        }
    }

    private static function createQuantitiesInput($dates, $pre_entered_values, $measures, $tab_number) {
        $tab_index = 0;
        $sub_index = 0;
        if ($pre_entered_values != null) {
            $dates = array_keys($pre_entered_values["administrated"][$measures[$tab_number]]);
        }
        foreach ($dates as $date) {
            $date = htmlspecialchars($date);
            echo "<tr>";
            if ($sub_index == 0) {
                echo "<td name='timeline'>Laikotarpis</td>";
                echo "<td id='quantities'>Suadministruota</td>";
                echo "</tr>";
                echo "<tr>";
            }
            echo "<td date='" . $date . "'>" . substr($date, 0, 4) . " " . substr($date, 5, 2) . "</td>";
            if ($pre_entered_values != null) {
                echo "<td id='date$date'_input><input name='administrated[n$tab_number][$date]' title='Suadminstruotų paraiškų skaičius' value='" . $pre_entered_values["administrated"][$measures[$tab_number]][$date] . "' onkeyup='nmDecimal(this)'/></td>";
            } else {
                echo "<td id='date$date'_input><input name='administrated[n$tab_number][$date]' title='Suadminstruotų paraiškų skaičius' onkeyup='nmDecimal(this)'/></td>";
            }
            echo "<tr>";
            $sub_index++;
        }
    }

}
